package study05;

import java.io.*;

public class Java03_Copy_huancun {
    public static void main(String[] args) throws Exception {
//        缓冲
//        数据源文件
        File srcFile = new File("D:\\java_study\\java_study\\data\\word.txt");
//        数据目的地文件(自动生成文件)
        File destFile = new File("D:\\java_study\\java_study\\data\\word.txt.copy");

        //    文件输入流
        FileInputStream in = null;
        //    文件输出流
        FileOutputStream out = null;

        //    缓冲输入流
        BufferedInputStream buffIn = null;
        //    缓冲输出流
        BufferedOutputStream buffOut = null;

//        缓冲区
        byte[] cache = new byte[1024];

        try {
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(destFile);

            buffIn = new BufferedInputStream(in);
            buffOut = new BufferedOutputStream(out);

//            打开阀门流转数据（输入）
            int data = -1;
//            打开阀门流转数据（输出）
//            out.write(data);
//            都缓冲区的数据
            while ((data = buffIn.read(cache)) != -1) {
//                写缓冲区的数据
                buffOut.write(cache, 0, data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (buffIn != null) {
                try {
                    buffIn.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (buffOut != null) {
                try {
                    buffOut.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
